package com.uinnova.product.eam.init.schedule.job;

import com.uinnova.product.eam.init.ocean.SyncOceanSvc;
import com.uinnova.project.base.diagram.util.RedisUtil;
import lombok.extern.log4j.Log4j2;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.quartz.QuartzJobBean;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

@Component
@Log4j2
public class OceanDataSyncJob extends QuartzJobBean {

    @Resource
    private RedisUtil redisUtil;

    @Value("${http.resource.space}")
    private String httpPath;

    @Resource
    private SyncOceanSvc syncOceanSvc;

    @Override
    protected void executeInternal(JobExecutionContext jobExecutionContext) throws JobExecutionException {

        // 获取同步锁
        String key = "lock:syncOceanDataSync:" + httpPath.replace(":", "");
        boolean lock = redisUtil.setnx(key, "1", 60, TimeUnit.SECONDS);
        if(!lock){
            log.info("获取锁失败");
            return;
        }
        try {
            syncOceanSvc.syncOceanData();
        } catch (Exception e) {
            log.error("oceanbase同步失败: {}", e, e.getMessage());
        } finally {
            // 删除同步锁
            redisUtil.del(key);
        }
    }
}
